﻿$(function () {
    query();

    //添加管理员
    $('.add-manager').click(function () {
        LoadAddBox();
    });
})
var pwdreg = /^[^\s]{6,20}$/;
function Delete(id) {
    $.dialog.confirm('确定删除该条记录吗？', function () {
        var loading = showLoading();
        $.post("./Delete", { id: id }, function (data) {
            loading.close();
            $.dialog.tips(data.msg); query()
        });
    });
}
function BatchDelete() {
    var selectedRows = $("#list").HimallDatagrid("getSelections");
    var selectids = new Array();

    for (var i = 0; i < selectedRows.length; i++) {
        selectids.push(selectedRows[i].Id);
    }
    if (selectedRows.length == 0) {
        $.dialog.tips("你没有选择任何选项！");
    }
    else {
        $.dialog.confirm('确定删除选择的管理员吗？', function () {
            var loading = showLoading();
            $.post("./BatchDelete", { ids: selectids.join(',') }, function (data) {
                loading.close();
                $.dialog.tips(data.msg); query()
            });
        });
    }
}

function query() {
    $("#list").HimallDatagrid({
        url: './list',
        nowrap: false,
        rownumbers: true,
        NoDataMsg: '没有找到符合条件的数据',
        border: false,
        fit: true,
        fitColumns: true,
        pagination: true,
        idField: "Id",
        pageSize: 10,
        pageNumber: 1,
        queryParams: {},
        toolbar: /*"#goods-datagrid-toolbar",*/'',
        operationButtons: "#batchOperate",
        columns:
        [[
            { checkbox: true, width: 39 },
            { field: "Id", hidden: true },
            { field: "Name", title: '名称' },
             {
                 field: "Image", title: '图标', align: 'center',
                 formatter: function (value, row, index) {
                     var html = "";
                     html += '<img  id="img' + row.Id + '" style="width="100" height="24" src="' + row.Image + '?' + Date() + '" />';
                     return html;
                 }
             },
            { field: "AppId", title: 'AppID' },
             { field: "AppSecret", title: 'AppSecret' },
              { field: "PlatOpenId", title: 'PlatOpenId' },
               { field: "Remark", title: '备注' },
        {
            field: "operation", operation: true, title: "操作",
            formatter: function (value, row, index) {
                var id = row.Id.toString();
                var Name = row.Name.toString();
                var Image = row.Image;
                var AppId = row.AppId;
                var AppSecret = row.AppSecret;
                var PlatOpenId = row.PlatOpenId;
                var Remark = row.Remark;
                var Phone = row.Phone;
                var model = JSON.stringify({ id: id, Name: Name, Image: Image, AppID: AppId, AppSecret: AppSecret, PlatOpenId: PlatOpenId, Remark: Remark, Phone: Phone });
                var html = ["<span class=\"btn-a\">"];
                if (row.RoleId != 0) {
                    html.push("<a onclick='Change(" + model + ");'>修改</a>");
                    html.push("<a onclick=\"Delete('" + id + "');\">删除</a>");
                }
                html.push("</span>");
                return html.join("");
            }
        }
        ]]
    });
}

function UploadInit(brandlogo) {
    $("#WxPic").hishopUpload(
    {
        title: '图标：',
        imageDescript: '建议上传50*50图标',
        displayImgSrc: brandlogo,
        dataWidth: 8,
        imagesCount: 1
    });
}

function LoadAddBox() {   
    $.dialog({
        title: '添加小程序',
        id: 'add',
        width: '500px',
        padding: '0 40px',
        content: document.getElementById("addWxSmallProgramform"),
        lock: true,
        okVal: '确认添加',
        init: function () { $("#Name").focus(); UploadInit(); },
        ok: function () {
            var name = $("#Name").val();
            var appID = $("#APPID").val();
            var appSecret = $("#AppSecret").val();
            var remark = $("#Remark").val();
            var phone = $("#Phone").val();
            var image = $('#WxPic').hishopUpload('getImgSrc').toString();
            if (name == null || name == "") {
                $.dialog.tips("名称必须填写！");
                return false;
            }
            if (appID == null || appID == "") {
                $.dialog.tips("appID必须填写！");
                return false;
            }
            if (appSecret == null || appSecret == "") {
                $.dialog.tips("appSecret必须填写！");
                return false;
            }
            if (phone == null || phone == "") {
                $.dialog.tips("客服电话必须填写！");
                return false;
            }
          
            if (!CheckAdd(appID))
                return false;
            AddSmall(name, appID, appSecret, image, remark,phone);
        }
    });
}

function Change(model) {
    var id=model.id;
    var Name=model.Name;
    var Image=model.Image;
    var AppID = model.AppID;
    var AppSecret = model.AppSecret;
    var Remark = model.Remark;
    var Phone = model.Phone;
    $.dialog({
        title: '修改小程序',
        lock: true,
        id: 'Change',
        width: '500px',
        content: document.getElementById("addWxSmallProgramform"),
        padding: '0 40px',
        okVal: '确定',
        init: function () {
            $("#AppSecret").focus();
            UploadInit(Image);
            $("#Name").val(Name);
            $("#APPID").val(AppID); $("#APPID").attr('readonly', 'readonly');
            $("#AppSecret").val(AppSecret);
            $("#Remark").val(Remark);
            $("#Phone").val(Phone);
        },
        ok: function () {
            var loading = showLoading();
            var rname = $("#Name").val();
            var rappID = $("#APPID").val();
            var rappSecret = $("#AppSecret").val();
            var phone = $("#Phone").val();
            var rremark = $("#Remark").val();
            var rimage = $('#WxPic').hishopUpload('getImgSrc').toString();
            if (rname == null || rname == "") {
                $.dialog.tips("名称必须填写！");
                return false;
            }
            if (rappID == null || rappID == "") {
                $.dialog.tips("appID必须填写！");
                return false;
            }
            if (rappSecret == null || rappSecret == "") {
                $.dialog.tips("appSecret必须填写！");
                return false;
            }          
            if (phone == null || phone == "") {
                $.dialog.tips("客服电话必须填写！");
                return false;
            }
            $.post("./Change",
                { id: id, Name: rname, appId: rappID, Image: rimage, AppSecret: rappSecret, Remark: rremark,Phone:phone },
                function (data) {
                    loading.close();
                    if (data.success) {
                        $.dialog.tips("修改成功", function () {
                            //if (roleid != 0 && roleid != SelectedRoleId)
                            query();
                        });
                        $("#password").val("");
                    }
                    else
                        $.dialog.tips("修改失败:" + data.msg);
                });
        }
    });
}

function CheckAdd(appID) {
   
    var loading = showLoading();
    var result = false;
    $.ajax({
        type: 'post',
        url: 'IsExists',
        cache: false,
        async: false,
        data: { appId: appID },
        dataType: "json",
        success: function (data) {
            loading.close();
            result = !data.Exists;
            if (data.Exists)
                $.dialog.tips("该AppId已存在！");
        },
        error: function () {
            loading.close();
        }
    });
    return result;
}

function AddSmall(name, appID, appSecret, image, remark,phone) {
    var loading = showLoading();
    $.ajax({
        type: 'post',
        url: 'Add',
        cache: false,
        async: true,
        data: { Name: name, appID: appID, appSecret: appSecret, image: image, remark: remark,Phone:phone },
        dataType: "json",
        success: function (data) {
            loading.close();
            if (data.success) {
                $.dialog.tips("添加成功！");
                $("#addWxSmallProgramform input").val("");
                query();
            }
            else {
                $.dialog.tips("添加失败！" + data.msg);
            }
        },
        error: function () {
            loading.close();
        }
    });
}